﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_DispatchEnd : System.Web.UI.Page
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        if (!IsPostBack)
        {
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            hidModuid.Value = Request.QueryString["moduid"];
            HidGoUrl.Value = Request.UrlReferrer.ToString();
            wcdate.Text = DateTime.Now.ToString();
            intData();//读取数据
            initUser();
        }
    }
    private void intData()//读取数据
    {
        string id = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(id))//新增
        {
            ZSql zs = new ZSql();
            zs.Open("select a.*,(select StaffName from CS_Staff where StaffId=a.maintmen) as StaffName from CS_Mailadd a where a.Maintaddid=" + Request.QueryString["id"]);

            lblDh.Text = zs.m_table.Rows[0]["MaintCode"].ToString();
            txtGuestName.Text = zs.m_table.Rows[0]["Guestname"].ToString();
            txtManName.Text = zs.m_table.Rows[0]["Guestmen"].ToString();
            txtAddress.Text = zs.m_table.Rows[0]["Guestaddress"].ToString();
            txtPhone.Text = zs.m_table.Rows[0]["Guesttel"].ToString();
            txtMaintDate.Text = zs.m_table.Rows[0]["MaintDate"].ToString();
            txtDispTime.Text = zs.m_table.Rows[0]["dispTime"].ToString();
            txtcomitype.Text = zs.m_table.Rows[0]["comitype"].ToString();
            txtBrand.Text = zs.m_table.Rows[0]["comidband"].ToString();
            txtSpeci.Text = zs.m_table.Rows[0]["comidSpe"].ToString();
            txtMaintTrouble.Text = zs.m_table.Rows[0]["MaintTrouble"].ToString();
            ddlName.SelectedValue = zs.m_table.Rows[0]["maintmen"].ToString();
            txtBespeakDate.Text = zs.m_table.Rows[0]["BespeakDate"].ToString();
            txtsjfj.Text = zs.m_table.Rows[0]["sjfj"].ToString();
            txtxlh1.Text = zs.m_table.Rows[0]["xlh1"].ToString();
            txtxlh2.Text = zs.m_table.Rows[0]["xlh2"].ToString();
            if (!string.IsNullOrEmpty(zs.m_table.Rows[0]["wcdate"].ToString()))
            {
                
                //结算信息
                ddlylyy.SelectedValue = zs.m_table.Rows[0]["ylyy"].ToString();
                txtylyy.Text = zs.m_table.Rows[0]["ylyybz"].ToString();//
                sertype.SelectedValue = zs.m_table.Rows[0]["fwlx"].ToString();
                ddlserfs.SelectedValue = zs.m_table.Rows[0]["jxfs"].ToString();
                ddlbxqk.SelectedValue = zs.m_table.Rows[0]["bxfs"].ToString();
                txtwxfy.Text = zs.m_table.Rows[0]["mailfy"].ToString();
                txtbjfy.Text = zs.m_table.Rows[0]["bjfy"].ToString();
                txtclfy.Text = zs.m_table.Rows[0]["clfy"].ToString();
                txtqtfy.Text = zs.m_table.Rows[0]["qtfy"].ToString();
                txttdfy.Text = zs.m_table.Rows[0]["tdfy"].ToString();
                txttc.Text = zs.m_table.Rows[0]["mailtc"].ToString();
                txtqyfysm.Text = zs.m_table.Rows[0]["qtfysm"].ToString();
                txtzr.Text = zs.m_table.Rows[0]["fyall"].ToString();
                ddlfwpf.SelectedValue = zs.m_table.Rows[0]["fwpf"].ToString();
                txtGuestYJ.Text = zs.m_table.Rows[0]["fwcs"].ToString();
                Guestcontent.Text = zs.m_table.Rows[0]["Guestcontent"].ToString();
                txtbjms.Text = zs.m_table.Rows[0]["bjms"].ToString();
                wcdate.Text = zs.m_table.Rows[0]["wcdate"].ToString();
                txtbjzyh.Text = zs.m_table.Rows[0]["bjzyh"].ToString();
            }

            zs.Close();

        }
    }

    //生成用户
    private void initUser()
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=40 order by StaffName");
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("—选派员工—", ""));
        name.Close();
    }
    //提交保存
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        ZSql info = new ZSql();
        string id = Request.QueryString["id"];
        string sql = "";
        sql = "select top 1 * from CS_Mailadd where Maintaddid=" + id;
        info.Open(sql);
        info.NextRow();

            info["Guestname"] = txtGuestName.Text;//客户名称
            info["Guestmen"] = txtManName.Text;//联系人
            info["Guestaddress"] = txtAddress.Text;//联系地址
            info["Guesttel"] = txtPhone.Text;//联系电话   
            info["MaintDate"] = txtMaintDate.Text;
            info["dispTime"] = txtDispTime.Text;//购买时间
            info["comitype"] = txtcomitype.Text;//型号类型
            info["comidband"] = txtBrand.Text;//品牌
            info["comidSpe"] = txtSpeci.Text;//型号
            info["MaintTrouble"] = txtMaintTrouble.Text;//报修故障
            info["maintmen"] = ddlName.SelectedValue;//维修人Id
            info["BespeakDate"] = txtBespeakDate.Text;//预约时间        
            info["sjfj"] = txtsjfj.Text;//随即附件
            info["xlh2"] = txtxlh2.Text;//序列号1
            info["xlh1"] = txtxlh1.Text;//序列号2

            info["ylyy"] = ddlylyy.SelectedValue;//
            info["ylyybz"] = txtylyy.Text;//
            info["fwlx"] = sertype.SelectedValue;//
            info["jxfs"] = ddlserfs.SelectedValue;//
            info["bxfs"] = ddlbxqk.SelectedValue;//
            info["mailfy"] = txtwxfy.Text;//   
            info["bjfy"] = txtbjfy.Text;
            info["clfy"] = txtclfy.Text;//
            info["qtfy"] = txtqtfy.Text;//
            info["tdfy"] = txttdfy.Text;//
            info["mailtc"] = txttc.Text;//
            info["qtfysm"] = txtqyfysm.Text;//
            info["fyall"] = txtzr.Text;//
            info["fwpf"] = ddlfwpf.SelectedValue;//        
            info["fwcs"] = txtGuestYJ.Text;//
            info["Guestcontent"] = Guestcontent.Text;//
            info["bjms"] = txtbjms.Text;//
            info["bjzyh"] = txtbjzyh.Text;//
            info["AuditState"] = "已通过";
            info["wcdate"] = wcdate.Text;
            info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
            info["EditDate"] = DateTime.Now.ToString();//时间
        info.Update();
        info.Close();

        string[] sArray = HidPjid.Value.ToString().Split(',');
        for (int i=0; i < sArray.Length-1; i++)
        {
            info.Open("update SV_OutStock set IsEnd=1,Maintaddid=" + id + " where OutStockId=" + sArray[i].ToString());
        }
        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='" + HidGoUrl.Value + "';</script>");//返回页面
    }
}
